//
//  CoreGraphicsVC.swift
//  BWExtensionKit_Example
//
//  Created by zhuxuhong on 2022/2/23.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import UIKit
import BWExtensionKit

class CustomView: UIView {
    override func draw(_ rect: CGRect) {
        guard let ctx = UIGraphicsGetCurrentContext() else {
            return
        }
        
        ctx.bw_drawLinearGradient(in: rect, locationAndColors: [
            0: .green,
            1: .blue
        ])
        
        return()
        ctx.setFillColor(UIColor.red.cgColor)
        
        ctx.saveGState()
        ctx.setFillColor(UIColor.yellow.cgColor)
        ctx.addEllipse(in: rect)
        ctx.drawPath(using: .fill)
        ctx.restoreGState()
        
        ctx.fill(rect.insetBy(dx: 40, dy: 80))
        
        ctx.setStrokeColor(UIColor.black.cgColor)
        ctx.setLineWidth(1)
        ctx.stroke(rect.insetBy(dx: 20, dy: 40))
        
        ctx.setStrokeColor(UIColor.purple.cgColor)
        ctx.setLineWidth(10)
        ctx.move(to: .init(x: rect.minX, y: rect.minY))
        ctx.addLine(to: .init(x: rect.maxX, y: rect.maxY))
        ctx.strokePath()
        
        let rPath1 = bw_createRoundedRectPath(with: [
            .topLeft: 10,
            .topRight: 20,
            .bottomLeft: 30,
            .bottomRight: 40
        ], for: rect.insetBy(dx: 100, dy: 100))
        ctx.setFillColor(UIColor.white.cgColor)
        ctx.addPath(rPath1)
        ctx.fillPath()
        
        let rPath2 = bw_createRoundedRectPath(with: [.all: 50], for: rect.insetBy(dx: 105, dy: 105))
        ctx.setStrokeColor(UIColor.black.cgColor)
        ctx.setLineWidth(10)
        ctx.setShadow(offset: .init(width: 0, height: 0),
                      blur: 20, color: UIColor.green.cgColor)
        ctx.addPath(rPath2)
        ctx.strokePath()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        setNeedsDisplay()
    }
}

class CoreGraphicsVC: UIViewController {
    override func loadView() {
        view = CustomView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
//        perform(#selector(hesitateUpdate), with: nil, afterDelay: 0.1)
    }
}
